<template>
  <div class="godds">
    <el-button type="primary" @click="addSelect">添加商品</el-button>
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form :model="form">
        <el-form-item label="请选择品牌">
          <el-select v-model="form.region" :label-width="formLabelWidth">
            <el-option
              :label="item.brandName"
              :value="item.id"
              v-for="item in selectList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-input v-model="form.proName" autocomplete="off" placeholder="请输入产品名称"></el-input>
        <el-input v-model="form.proDesc" autocomplete="off" placeholder="请输入产品描述"></el-input>
        <el-input v-model="form.num" autocomplete="off" placeholder="请输入产品数量"></el-input>
        <el-input v-model="form.size" autocomplete="off" placeholder="请输入产品尺码，如20-39"></el-input>
        <el-input v-model="form.oldPrice" autocomplete="off" placeholder="请输入产品原价"></el-input>
        <el-input v-model="form.price" autocomplete="off" placeholder="请输入产品折扣价"></el-input>
        <el-form-item>
          <el-upload
            action="http://127.0.0.1:3000/product/addProductPic"
            :with-credentials="true"
            name="pic1"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt>
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addupload">确 定</el-button>
      </div>
    </el-dialog>
    <el-table
      ref="multipleTable"
      :data="tableData3"
      tooltip-effect="dark"
      style="width: 100%"
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <!-- <el-table-column type="index" width="50"></el-table-column> -->
      <el-table-column prop="id" label="商品编号" width="100" align="center"></el-table-column>
      <el-table-column prop="proName" label="商品名称" width="610" align="center"></el-table-column>
      <el-table-column prop="proDesc" label="商品描述" width="150" align="center"></el-table-column>
      <el-table-column prop="num" label="商品库存" width="100" align="center"></el-table-column>
      <el-table-column prop="size" label="商品尺寸" width="100" align="center"></el-table-column>
      <el-table-column prop="brandId" label="是否下架" show-overflow-tooltip width="100" align="center">
        <template slot-scope="scope">{{scope.row.brandId | startUser}}</template>
      </el-table-column>
      <el-table-column label="操作" width="60" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            :type="scope.row.brandId ? 'success' : 'danger' "
            @click="handleChange(scope.row)"
          >{{scope.row.brandId ? '上架' : '下架'}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="CurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="this.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import { goodDetail, addGood, SecondCate, addGoodPic } from '@/api'
export default {
  created () {
    this.initList()
  },
  data () {
    return {
      // 当前页
      currentPage4: 1,
      total: 100,
      pagesize: 5,
      tableData3: [],
      multipleSelection: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      dialogImageUrl: '',
      dialogVisible: false,
      // 下拉列表
      selectList: [],

      form: {
        proName: '',
        proDesc: '',
        num: '',
        size: '',
        oldPrice: '',
        price: '',
        addPicName: '',
        addPicAddr: '',
        region: ''
      },
      formLabelWidth: '30px'
    }
  },
  filters: {
    startUser (val) {
      if (val === 1) {
        return '已下架商品'
      } else {
        return '已上架商品'
      }
    }
  },
  methods: {
    // 初始化页面
    initList () {
      goodDetail({ page: this.currentPage4, pageSize: this.pagesize })
        .then(res => {
          console.log(res)
          this.total = res.data.total
          this.tableData3 = res.data.rows
        })
    },
    // 添加框
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => { })
    },
    // // 新增商品的下拉菜单
    // addSelect () {
    //   goodDetail({ page: this.currentPage4, pageSize: this.pagesize })
    //     .then(res => {
    //       console.log(res)
    //       this.total = res.data.total
    //       this.tableData3 = res.data.rows
    //     })
    // },
    // 新增商品
    addupload () {
      this.dialogFormVisible = false
      addGood(this.form)
        .then(res => {
          console.log(res)
          this.initList()
        })
    },
    // 新增商品下拉
    addSelect () {
      this.dialogFormVisible = true
      SecondCate({ page: 1, pageSize: 50 })
        .then(res => {
          console.log(res)
          this.selectList = res.data.rows
        })
    },
    // 上传图片
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handlesuccess (res) {
      console.log(res.picAddr)
      this.addPicName = res.picName
      this.addPicAddr = res.picAddr
    },
    // 页码与表格
    toggleSelection (rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    // 变换商品上下架状态
    handleChange (val) {
      console.log(val)
      console.log('没接口')
    },
    setCurrent (row) {
      this.$refs.singleTable.setCurrentRow(row)
    },
    handleCurrentChange (val) {
      this.currentRow = val
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.initList()
    },
    CurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.currentPage4 = val
      this.initList()
    }
  }
}
</script>
<style lang="scss" scoped>
.el-table::before {
  height: 0;
}
.el-input {
  margin-bottom: 15px;
}
</style>
